You'll be using the Dark Sky Forecast API from Forecast.io, available at https://developer.forecast.io. It's a pretty simple API, but be sure to read the documentation!
A forecast request returns the current forecast (for the next week): https://api.forecast.io/forecast/APIKEY/LATITUDE,LONGITUDE
A time-machine request returns the observed weather at a given time (for many places, up to 60 years in the past): https://api.forecast.io/forecast/APIKEY/LATITUDE,LONGITUDE,TIME
1) Make a request from the Forecast.io API for where you were born (or lived, or want to visit!).
Graded = 7/7
In [1]:
import requests
In [2]:
response = requests.get("https://api.forecast.io/forecast/e554f37a8164ce189acd210d00a452e0/47.4079,9.4647")
weather_data = response.json()
weather_data.keys()
Out[2]:
In [ ]:
In [3]:
print(weather_data['timezone'])
The time zone of Trogen is correct! This is where I live.
In [4]:
print("Longitude:", weather_data['longitude'], "Latitude", weather_data['latitude'])
The longitude is mentioned first, and then the latitude. Usually, it is the other way round.
2) What's the current wind speed? How much warmer does it feel than it actually is?
In [5]:
type(weather_data['currently'])
Out[5]:
In [6]:
weather_data['currently'].keys()
Out[6]:
In [40]:
print("The wind in Trogen, Switzerland, is currently blowing at", weather_data['currently']['windSpeed'], "mph.")
In [7]:
weather = weather_data['currently']
Temperature = int(weather['apparentTemperature']) - int(weather['temperature'])
Celsius = round((int(weather['temperature']) - 32) * 5 / 9)
if Temperature == 0:
print("The temperature feels exactly as you would expect it to feel, namely", weather['temperature'], "degrees Fahrenheit, or", Celsius, "degrees Celsius.")
elif Temperature > 0:
print("It feels", Temperature, "degrees Fahrenheit warmer than the actual temperature, which is", weather['temperature'], "degrees Fahrenheit, or", Celsius, "degrees Celsius.")
else:
print("It feels", Temperature, "degrees Fahrenheit colder than the actual temperature, which is", weather['temperature'], "degrees Fahrenheit, or", Celsius, "degrees Celsius.")
3) The first daily forecast is the forecast for today. For the place you decided on up above, how much of the moon is currently visible?
In [8]:
type(weather_data['daily'])
Out[8]:
In [43]:
weather_data['daily'].keys()
Out[43]:
In [44]:
type(weather_data['daily']['data'])
Out[44]:
In [9]:
for phase in weather_data['daily']['data']:
moon_forecast_tomorrow = phase['moonPhase']
break
#print(phase['moonPhase'])
if moon_forecast_tomorrow == 0:
print("Tomorrow is New Moon.")
elif moon_forecast_tomorrow > .75:
print("Tomorrow the Moon is in a Waning Crescent phase.")
elif moon_forecast_tomorrow == .75:
print("Tomorrow is last quarter Moon.")
elif moon_forecast_tomorrow > .5:
print("Tommorrow the Moon is in a Waning Gibbous phase.")
elif moon_forecast_tomorrow == .5:
print("Tommorrow is Full Moon.")
elif moon_forecast_tomorrow > .25:
print("Tommorrow the Moon is a Waxing Gibbous phase.")
elif moon_forecast_tomorrow == .25:
print("Tommorrow is first Quarter Moon.")
elif moon_forecast_tomorrow > 0:
print("Tommorrow the Moon is in a Waxing Crescent phase. This is the first phase after New Moon.")
4) What's the difference between the high and low temperatures for today?
In [46]:
print(weather_data['currently'])
In [12]:
response = requests.get("https://api.forecast.io/forecast/e554f37a8164ce189acd210d00a452e0/47.4079,9.4647,1465420000")
hist_weather_data = response.json()
hist_weather_data.keys()
Out[12]:
In [48]:
hist_weather_data['daily'].keys()
Out[48]:
In [49]:
print(hist_weather_data['daily']['data'])
In [13]:
for n in hist_weather_data['daily']['data']:
Min = n['temperatureMin']
Max = n['temperatureMax']
In [14]:
Min_Max = Max - Min
Min_Max_Celsius = ((Max - 32) * 5 / 9) - ((Min - 32) * 5 / 9)
print("The diffrence between the high and low temperatures on Wedesday, June 8, in Trogen, Switzerland, was", round(Min_Max), "Fahrenheit", "or", round(Min_Max_Celsius), "Celsius.")
5) Loop through the daily forecast, printing out the next week's worth of predictions. I'd like to know the high temperature for each day, and whether it's hot, warm, or cold, based on what temperatures you think are hot, warm or cold.
In [52]:
response = requests.get("https://api.forecast.io/forecast/e554f37a8164ce189acd210d00a452e0/40.7128,-74.0059")
weather_data = response.json()
weather_data.keys()
Out[52]:
In [53]:
weather_data['daily'].keys()
Out[53]:
In [60]:
print(weather_data['daily']['data'][0])
In [15]:
Forecast = weather_data['daily']['data']
for max_temp in Forecast:
Convert_Celsius = (max_temp['temperatureMax'] - 32) * 5 / 9
if Convert_Celsius > 30:
Temperature = "hot"
elif Convert_Celsius > 20:
Temperature = "warm"
else:
Temperature = "cold"
print("The maximum temperature in New York for next week are", max_temp['temperatureMax'], "Fahrenheit or", round(Convert_Celsius), "Celsius. That is", Temperature)
In [62]:
import datetime
print(datetime.datetime.fromtimestamp(int("1284101485")).strftime('%Y-%m-%d'))
In [16]:
import time
import datetime
Forecast = weather_data['daily']['data']
for max_temp in Forecast:
Convert_Celsius = (max_temp['temperatureMax'] - 32) * 5 / 9
time1 = max_temp['time']
if Convert_Celsius > 30:
Temperature = "hot"
elif Convert_Celsius > 20:
Temperature = "comfortably warm."
else:
Temperature = "cold"
#print("On", time.strftime('%A, %b %d', time.localtime(time1)), "the maximum temperature will be", max_temp['temperatureMax'], "Fahrenheit or", round(Convert_Celsius), "Celsius. That is", Temperature)
print("On", time.strftime('%A', time.localtime(time1)), "the maximum temperature will be", max_temp['temperatureMax'], "Fahrenheit or", round(Convert_Celsius), "Celsius. That is", Temperature)
In [17]:
import datetime
print(datetime.datetime.fromtimestamp(int(time)).strftime('%Y-%m-%d'))
6) What's the weather looking like for the rest of today in Miami, Florida? I'd like to know the temperature for every hour, and if it's going to have cloud cover of more than 0.5 say "{temperature} and cloudy" instead of just the temperature.
In [18]:
response = requests.get("https://api.forecast.io/forecast/e554f37a8164ce189acd210d00a452e0/25.7617,-80.1918")
Florida = response.json()
Florida.keys()
Out[18]:
In [74]:
Florida['hourly'].keys()
Out[74]:
In [75]:
#print(Florida['hourly']['data'])
In [19]:
import time
import datetime
Hourly = Florida['hourly']['data']
Hourly = Hourly
for weather in Hourly:
time = weather['time']
stop_time = datetime.datetime.fromtimestamp(int(time)).strftime('%H')
if stop_time == '01':
break
if weather['cloudCover'] > 0.5:
cloudy = "and cloudy"
else:
cloudy = "not cloudy"
print(datetime.datetime.fromtimestamp(int(time)).strftime('%H:%M'), "{", weather['temperature'], "°F}",cloudy)
7) What was the temperature in Central Park on Christmas Day, 1980? How about 1990? 2000?
In [77]:
response = requests.get("https://api.forecast.io/forecast/e554f37a8164ce189acd210d00a452e0/40.781750,-73.966641,346593600")
weather_data = response.json()
weather_data.keys()
Out[77]:
In [78]:
#print(weather_data['daily']['data'][0])
In [79]:
for Christmas in weather_data['daily']['data']:
Convert_Celsius = (Christmas['temperatureMax'] - 32) * 5 / 9
print("The maximum temperature on Christmas Day 1980 in Centralpark was",
Christmas['temperatureMax'], "Fahrenheit, or", round(Convert_Celsius), "degrees Celsius.")
In [80]:
response = requests.get("https://api.forecast.io/forecast/e554f37a8164ce189acd210d00a452e0/40.781750,-73.966641,662126400")
weather_data_1990 = response.json()
weather_data_1990.keys()
response = requests.get("https://api.forecast.io/forecast/e554f37a8164ce189acd210d00a452e0/40.781750,-73.966641,977745600")
weather_data_2000 = response.json()
weather_data_2000.keys()
Out[80]:
In [81]:
for Christmas in weather_data_1990['daily']['data']:
Convert_Celsius = (Christmas['temperatureMax'] - 32) * 5 / 9
print("The maximum temperature on Christmas Day 1990 in Centralpark was",
Christmas['temperatureMax'], "Fahrenheit, or", round(Convert_Celsius), "degrees Celsius.")
In [82]:
for Christmas in weather_data_2000['daily']['data']:
Convert_Celsius = (Christmas['temperatureMax'] - 32) * 5 / 9
print("The maximum temperature on Christmas Day 1980 in Centralpark was",
Christmas['temperatureMax'], "Fahrenheit, or", round(Convert_Celsius), "degrees Celsius.")
In [ ]: